package com.app.aipass.view;

import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;

/**
 * @author: woking
 * @date: 2021/6/11
 * @description：
 */
public class MyTextView extends TextView implements View.OnClickListener
{

    private boolean isFalag = true;
    public MyTextView(Context context) {
        super(context);
        initData();
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initData();
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initData();
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initData();
    }

    private void initData()
    {
        if(isFalag)
        {
            setClickZoomEffect(this,1.5f);
        }
    }

    @Override
    public void onClick(View view) {
        if(isFalag)
        {
            setClickZoomEffect(this,1.5f);
            isFalag = false;
        }
        else
        {
            setClickZoomEffect(this,1.0f);
            isFalag = true;
        }

    }

    public void setClickZoomEffect(final View view, float flag) {

        if (view != null) {
            view.setOnTouchListener(new OnTouchListener() {
                boolean cancelled;
                Rect rect = new Rect();
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            scaleTo(v, flag);
                            break;
//                        case MotionEvent.ACTION_MOVE:
//                            if (rect.isEmpty()) {
//                                v.getDrawingRect(rect);
//                            }
//                            if (!rect.contains((int) event.getX(), (int) event.getY())) {
//                                scaleTo(v, flag);
//                                cancelled = true;
//                            }
//                            break;
//                        case MotionEvent.ACTION_UP:
//                        case MotionEvent.ACTION_CANCEL: {
//                            if (!cancelled) {
//                                scaleTo(v, flag);
//                            } else {
//                                cancelled = false;
//                            }
//                            break;
//                        }
                    }
                    return false;
                }
            });
        } }
    public static void scaleTo(View v, float scale) {
        if (Build.VERSION.SDK_INT >= 11) {
            v.setScaleX(scale);
            v.setScaleY(scale);
        } else {
            float oldScale = 1;
            if (v.getTag(Integer.MIN_VALUE) != null) {
                oldScale = (Float) v.getTag(Integer.MIN_VALUE);
            }
            ViewGroup.LayoutParams params = v.getLayoutParams();
            params.width = (int) ((params.width / oldScale) * scale);
            params.height = (int) ((params.height / oldScale) * scale);
            v.setTag(Integer.MIN_VALUE, scale);
        }
    }
}
